from django.db import models

# Create your models here.
from django.utils.html import format_html


class Category(models.Model):
    name = models.CharField(max_length=20, verbose_name="分类名称")
    cover = models.URLField(verbose_name="分类封面")

    class Meta:
        verbose_name = verbose_name_plural = "静态壁纸分类"

    def __str__(self):
        return self.name


class Wallpaper(models.Model):
    VERTICAL = 0
    HORIZONTAL = 1
    ORIENTATION = (
        (VERTICAL, "竖屏"),
        (HORIZONTAL, "横屏"),
    )
    title = models.CharField(max_length=100, verbose_name="壁纸标题")
    category = models.ForeignKey(Category, verbose_name="壁纸分类", null=True, on_delete=models.SET_NULL)
    tag = models.CharField(max_length=100, verbose_name="壁纸标签")
    url = models.URLField(unique=True, verbose_name="壁纸链接")
    resolution = models.CharField(max_length=20, verbose_name="壁纸图片尺寸")
    file_size = models.CharField(max_length=20, verbose_name="壁纸图片大小")
    create_time = models.DateTimeField(verbose_name="壁纸创建时间", null=True)
    orientation = models.IntegerField(choices=ORIENTATION, default=VERTICAL, verbose_name="壁纸方向")

    class Meta:
        verbose_name = verbose_name_plural = "静态壁纸"

    def toDict(self):
        return {
            'id': self.id, 'title': self.title, 'category': self.category.name, 'tag': self.tag, 'url': self.url,
            'resolution': self.resolution, 'file_size': self.file_size, 'create_time': self.create_time,
        }

    def image_display(self):
        return format_html('<img src="{}" width="30%" height="auto"/>', self.url)

    image_display.short_description = '壁纸'

    def __str__(self):
        return self.title


class LiveCategory(models.Model):
    name = models.CharField(max_length=20, verbose_name="分类名称")
    cover = models.URLField(verbose_name="分类封面")

    class Meta:
        verbose_name = verbose_name_plural = "动态壁纸分类"

    def __str__(self):
        return self.name


class LiveWallpaper(models.Model):
    VERTICAL = 0
    HORIZONTAL = 1
    ORIENTATION = (
        (VERTICAL, "竖屏"),
        (HORIZONTAL, "横屏"),
    )
    title = models.CharField(max_length=100, verbose_name="壁纸标题")
    category = models.ForeignKey(LiveCategory, verbose_name="壁纸分类", null=True, on_delete=models.SET_NULL)
    tag = models.CharField(max_length=100, verbose_name="壁纸标签")
    preview = models.URLField(unique=True, verbose_name="动态壁纸预览")
    url = models.URLField(unique=True, verbose_name="动态壁纸链接")
    resolution = models.CharField(max_length=20, verbose_name="壁纸图片尺寸")
    file_size = models.CharField(max_length=20, verbose_name="壁纸图片大小")
    create_time = models.DateTimeField(verbose_name="壁纸创建时间", null=True)
    orientation = models.CharField(max_length=4, choices=ORIENTATION, default=VERTICAL, verbose_name="壁纸方向")

    class Meta:
        verbose_name = verbose_name_plural = "动态壁纸"

    def toDict(self):
        return {
            'id': self.id, 'title': self.title, 'category': self.category.name, 'tag': self.tag,
            'preview': self.preview,
            'url': self.url, 'resolution': self.resolution, 'file_size': self.file_size,
            'create_time': self.create_time,
        }

    def __str__(self):
        return self.title
